package spider

import (
	"fmt"
	"log"
	"strconv"
	"strings"
	"study/model"
	"study/service"
	"time"

	"github.com/gocolly/colly"
)

/*
诗词爬虫
*/
var PoetrySpider poetrySpider = poetrySpider{}

type poetrySpider struct {
}

func (this poetrySpider) Execute() {
	c := newCollector()
	// spiderDynasty(c)
	spiderAuthor(c)
}

func newCollector() *colly.Collector {
	c := colly.NewCollector(
		colly.Async(false),
	)
	c.Limit(&colly.LimitRule{
		// Delay: 5 * time.Second,
		Parallelism: 2,
		RandomDelay: 5 * time.Second,
	})

	c.OnRequest(func(r *colly.Request) {
		fmt.Println("Visiting", r.URL)
	})

	c.OnError(func(_ *colly.Response, err error) {
		log.Println("Something went wrong:", err)
	})

	c.OnResponse(func(r *colly.Response) {
		fmt.Println("Visited", r.Request.URL)
	})

	return c
}

// 爬取朝代
func spiderDynasty(c *colly.Collector) {
	var url string = "http://www.zhongguoshici.com/zuozhe/list"
	c.OnHTML("div[class='type_list']", func(e *colly.HTMLElement) {
		e.ForEach("a", func(i int, item *colly.HTMLElement) {
			dynasty := item.Text
			isExist := service.DynastyService.IsExistByName(dynasty)
			if isExist {
				log.Println("数据已存在，dynasty=", dynasty)
			} else {
				log.Println("添加数据，dynasty=", dynasty)
				service.DynastyService.Add(&model.Dynasty{Name: dynasty})
			}
		})
	})

	c.Visit(url)
	c.Wait()
}

// 爬取作者
func spiderAuthor(c *colly.Collector) {
	region := "http://www.zhongguoshici.com"
	var baseUrl string = region + "/zuozhe/list"
	//TODO i实际=930
	for i := 1; i <= 5; i++ {
		var url string = baseUrl
		if i > 1 {
			url = baseUrl + "?p=" + strconv.Itoa(i)
		}
		c.OnHTML("ul[class='item_list']", func(e *colly.HTMLElement) {
			e.ForEach("a", func(i int, item *colly.HTMLElement) {
				href := item.Attr("href")                        //详情url
				img := item.ChildAttr("img", "src")              //头像url
				writer := item.ChildText("span[class='writer']") //作者
				baseDynasty := item.ChildText("span[class='dynasty']")
				dynasty := strings.ReplaceAll(baseDynasty, "朝代：", "") //朝代
				// content := item.ChildText("div[class='writer_content']") //作者简介
				log.Println(href, img, writer, dynasty)
				//TODO 下载头像

				infoUrl := region + href

				infoc := newCollector()
				infoc.OnHTML("div[class='writer_detail']", func(e *colly.HTMLElement) {
					var content string
					var life string
					var achievement string
					e.ForEach("div[class='detial_frame']", func(i int, item *colly.HTMLElement) {
						// log.Println("========================进入获取详情")
						if i == 1 {
							content = item.ChildText("div[class='writer_content']")
							log.Println("========================content", content)
						} else if i == 2 {
							life = item.Text
							log.Println("========================life", life)
						} else if i == 3 {
							achievement = item.Text
							log.Println("========================achievement", achievement)
						}
					})
				})
				infoc.Visit(infoUrl)
			})
		})
		c.Visit(url)
		// time.Sleep(5 * time.Second)
	}
}
